<?php

/**
 ** HOW TO USE:
 * Step 1: Define actions
 * 
 * class SiteController extends Controller {
        public function actions()
        {
                return array(
                        'mce_blank' => array(
                            'class' => 'ext.tinymce.BlankAction'
                        ),
                        'mce_upload' => array(
                            'class' => 'ext.tinymce.UploadAction'
                        )
                );
        }
 * }
 * 
 * Step 2: Edit config file 
 * 
 * $config['action_module'] = ''; //module name of action
 * $config['action_controller'] = 'site'; //controller of action
 * $config['action_blank'] = 'mce_blank'; //action name
 * $config['action_upload'] = 'mce_upload'; //action name

    Step 3: Using
    <textarea id="tinymce_test"></textarea>

   <?php $this->widget('ext.tinymce.TinyMCE', array(
       'selector' => '#tinymce_test',
   ));?>
 */
class TinyMCE extends CWidget {
    
    public $libUrl = '';
    public $selector = 'textarea';
    public $options = null;
    
    public function init() {
        if(session_id() == '') {
            session_start();
        }
        
        $config = include(dirname(__FILE__) . '/config.php');
        $mod = $config['action_module'];
        $con = $config['action_controller'];
        $blank = $config['action_blank'];
        $upload = $config['action_upload'];
        
        $_SESSION['tinymce_upload_action_upload'] = YiI::app()->createUrl((isset($mod) ? '/' . $mod . '/' : '') . $con . '/' . $upload);
        $_SESSION['tinymce_upload_action_blank'] = YiI::app()->createUrl((isset($mod) ? '/' . $mod . '/' : '') . $con . '/' . $blank);
        
        if (empty($this->libUrl)) {
            $this->libUrl = Yii::app()->assetManager->publish(Yii::getPathOfAlias('ext.tinymce.js.tinymce'));
        }
        
        if (is_null($this->options)) {
            $this->options = array();
            $this->options['plugins'] = array(
                "advlist autolink lists link image charmap print preview anchor",
                "searchreplace visualblocks code fullscreen",
                "insertdatetime media table contextmenu paste jbimages",
            );
            $this->options['toolbar'] = "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image jbimages";
            $this->options['relative_urls'] = false;
            
            $this->options['entity_encoding'] = "raw";
            $this->options['verify_html'] = "false";
            $this->options['valid_elements'] = "*[*]";
            $this->options['extended_valid_elements'] = "*[*]";
        }
        
        $this->options['selector'] = $this->selector;
    }
    
    public function run() {
        Yii::app()->clientScript->registerCoreScript('jquery');
        Yii::app()->clientScript->registerScriptFile($this->libUrl . '/tinymce.min.js');
        Yii::app()->clientScript->registerScript('tinymce_init', 'tinymce.init(' . CJavaScript::encode($this->options) . ');', CClientScript::POS_READY);
    }
}